package vip.zhenzicheng.nettybasic.outread;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

import java.util.logging.Logger;

/**
 * 服务端的业务Handler
 *
 * @author zhenzicheng
 * @date 2022-06-07 19:30
 */
public class EchoServerORHandler extends ChannelInboundHandlerAdapter {

  private static final Logger LOG = Logger.getLogger(EchoServerORHandler.class.getSimpleName());

  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    LOG.info("server accept：" + in.toString(CharsetUtil.UTF_8));
    ctx.writeAndFlush(in);
  }

  @Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
    LOG.info("连接已建立");
    super.channelActive(ctx);
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    cause.printStackTrace();
    ctx.close();
  }
}
